﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Diagnostics;
using System.IO;

namespace WOWMultiPlay
{
    public partial class ChooseInstanceForm : Form
    {
        public ChooseInstanceForm()
        {
            InitializeComponent();
        }

        private void ChooseInstanceForm_Load(object sender, EventArgs e)
        {
            LoadInstances();
        }

        private void LoadInstances()
        {
            if (!IsLoading)
            {
                IsLoading = true;

                DataTable DT = new DataTable();
                DT.Columns.Add("ProcessDisplayTitle");
                DT.Columns.Add("ProcessMainWindowHandle");

                Process[] Processes = Process.GetProcessesByName(Setting_Global.ProcessName);

                foreach (Process P in Processes)
                {
                    DataRow DR = DT.NewRow();
                    DR["ProcessDisplayTitle"] = P.MainWindowTitle + " <" + P.MainWindowHandle + ">";
                    DR["ProcessMainWindowHandle"] = P.MainWindowHandle;

                    DT.Rows.Add(DR);
                }

                //Get other profiles which are already running
                DirectoryInfo DictInfo = new DirectoryInfo(Setting_Global.AppPath);
                FileInfo[] FilesInfo = DictInfo.GetFiles("*." + Setting_Global.RunningProfileTempFileSuffix);

                if (FilesInfo.Length > 0)
                {
                    foreach (FileInfo FileInfo in FilesInfo)
                    {
                        DataRow[] DRs = DT.Select("ProcessMainWindowHandle = '" + FileInfo.Name.Replace("." + Setting_Global.RunningProfileTempFileSuffix, "") + "'");
                        if (DRs.Length > 0)
                        {
                            StreamReader SR = FileInfo.OpenText();
                            DRs[0]["ProcessDisplayTitle"] += " - " + SR.ReadToEnd();
                            SR.Close();
                        }
                    }
                }

                listBox_WOWInstances.DataSource = DT;
                listBox_WOWInstances.DisplayMember = "ProcessDisplayTitle";
                listBox_WOWInstances.ValueMember = "ProcessMainWindowHandle";

                IsLoading = false;
            }
        }

        private void listBox_WOWInstances_DoubleClick(object sender, EventArgs e)
        {
            if (listBox_WOWInstances.SelectedItems.Count > 0)
            {
                this.DialogResult = DialogResult.OK;
                Setting_Global.Instance = new IntPtr(Convert.ToInt32(listBox_WOWInstances.SelectedValue));
                Setting_Global.LoadedInstance = true;
                this.Close();
            }
        }

        bool IsLoading = false;

        private void timer_LoadInstances_Tick(object sender, EventArgs e)
        {
            //LoadInstances();
        }
    }
}